﻿module Supernova {

    export class Location {

        public x: number = 0;
        public y: number = 0;
        public ship: Ship = null;
        public body: Body = null;
        public system: StarSystem = null;

        clear() {
            this.x = 0;
            this.y = 0;
            this.ship = null;
            this.body = null;
            this.system = null;
        }

        detach() {
            this.ship = null;
            this.body = null;
            this.system = null;
        }

        setXY(x: number, y: number) {
            this.clear();
            this.x = x;
            this.y = y;
        }

        setShip(ship: Ship) {
            this.clear();
            this.ship = ship;
            this.updateXY();
        }

        setBody(body: Body) {
            this.clear();
            this.body = body;
            this.updateXY();
        }

        setSystem(system: StarSystem) {
            this.clear();
            this.system = system;
            this.updateXY();
        }

        updateXY() {
            if (this.ship !== null) {
                this.x = this.ship.currentLocation.x;
                this.y = this.ship.currentLocation.y;
            }
            else if (this.body !== null) {
                this.x = this.body.x;
                this.y = this.body.y;
            }
            else if (this.system !== null) {
                this.x = this.system.x;
                this.y = this.system.y;
            }
        }

    }

}